package com.cskaoyan.market.service;

import com.cskaoyan.market.db.domain.MarketBrand;
import com.cskaoyan.market.db.domain.MarketBrandExample;
import com.cskaoyan.market.db.mapper.MarketBrandMapper;
import com.cskaoyan.market.util.MybatisUtils;
import com.github.pagehelper.PageHelper;
import org.apache.commons.lang3.StringUtils;
import org.apache.ibatis.session.SqlSession;

import java.math.BigDecimal;
import java.time.LocalDateTime;
import java.util.List;

public class AdminBrandServiceImpl implements AdminBrandService {


    @Override
    public List<MarketBrand> list(String page, String limit, String id, String name, String sort, String order) {
        SqlSession session = MybatisUtils.getSession();
        MarketBrandMapper marketBrandMapper = session.getMapper(MarketBrandMapper.class);
        MarketBrandExample marketBrandExample = new MarketBrandExample();
        MarketBrandExample.Criteria criteria = marketBrandExample.createCriteria();
        if(!StringUtils.isEmpty(id)){
            criteria.andIdEqualTo(Integer.parseInt(id));
        }
        if(!StringUtils.isEmpty(name)){
            criteria.andNameLike("%"+name+"%");
        }
        marketBrandExample.setOrderByClause(sort + " " + order);
        PageHelper.startPage(Integer.parseInt(page), Integer.parseInt(limit));
        List<MarketBrand> marketBrands = marketBrandMapper.selectByExample(marketBrandExample);
        session.close();
        return marketBrands;
    }

    @Override
    public MarketBrand create(String desc, String floorPrice, String name, String picUrl) {
        SqlSession session = MybatisUtils.getSession();
        MarketBrandMapper marketBrandMapper = session.getMapper(MarketBrandMapper.class);
        MarketBrand marketBrand = new MarketBrand();
        marketBrand.setAddTime(LocalDateTime.now());
        marketBrand.setUpdateTime(LocalDateTime.now());
        marketBrand.setDesc(desc);
        marketBrand.setFloorPrice(BigDecimal.valueOf(Long.parseLong(floorPrice)));
        marketBrand.setName(name);
        marketBrand.setPicUrl(picUrl);
        marketBrandMapper.insert(marketBrand);
        session.commit();

        MarketBrandExample marketBrandExample = new MarketBrandExample();
        MarketBrandExample.Criteria criteria = marketBrandExample.createCriteria();
        criteria.andPicUrlEqualTo(marketBrand.getPicUrl());
        MarketBrand resulMaketBrand = marketBrandMapper.selectOneByExample(marketBrandExample);
        session.close();

        return resulMaketBrand;

    }

    @Override
    public MarketBrand update(MarketBrand marketBrand) {
        SqlSession session = MybatisUtils.getSession();
        MarketBrandMapper marketBrandMapper = session.getMapper(MarketBrandMapper.class);
        MarketBrandExample marketBrandExample = new MarketBrandExample();
        MarketBrandExample.Criteria criteria = marketBrandExample.createCriteria();
        criteria.andIdEqualTo(marketBrand.getId());
        marketBrandMapper.updateByExampleSelective(marketBrand,marketBrandExample);
        session.commit();
        MarketBrand result = marketBrandMapper.selectOneByExample(marketBrandExample);
        session.close();

        return result;


    }

    @Override
    public void delete(String id) {
        SqlSession session = MybatisUtils.getSession();
        MarketBrandMapper marketBrandMapper = session.getMapper(MarketBrandMapper.class);
        MarketBrandExample marketBrandExample = new MarketBrandExample();
        MarketBrandExample.Criteria criteria = marketBrandExample.createCriteria();
        criteria.andIdEqualTo(Integer.parseInt(id));
        marketBrandMapper.deleteByExample(marketBrandExample);
        session.commit();
        session.close();
    }
}
